﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using elr_tracking_wpf.UI;
using elr_tracking_bal.ART;
using elr_tracking_bal.CAT;
using elr_tracking_bal.POM;
using elr_tracking_bal;

namespace elr_tracking_wpf
{
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        UserControl test;
        private bool ban =false;
        Button Guardar = new Button();
        Button Guardar_Salir = new Button();

        private pom _despliega;

        public pom Despliega1
        {
            get { return _despliega; }
            set { _despliega = value; }
        }

        private UIGrid _despliega2;
        public UIGrid Despliega2
        {
            get { return _despliega2; }
            set { _despliega2 = value; }
        }

        public MainWindow()
        {
            InitializeComponent();
            test = new UserControl();
            button1.Visibility = Visibility.Hidden;
            Guardar.Content = "Guardar";
            Guardar.Height = 30;
            Guardar.Width = 75;
            Guardar.Margin = new Thickness(-170, 0, 0, 0);
            Guardar.Click += new RoutedEventHandler(Guardar_Click);
            Guardar.Visibility = Visibility.Visible;
            Guardar_Salir.Content = "Guardar y salir";
            Guardar_Salir.Height = 30;
            Guardar_Salir.Width = 175;
            Guardar_Salir.Margin = new Thickness(-30, 0, 0, 0);
            Guardar_Salir.Click += new RoutedEventHandler(Guardar_Salir_Click);
            Guardar_Salir.Visibility = Visibility.Visible;
            //sp.Children.Add(test);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //this.svContainer.Content = new UIReciboGrid();
            //sp.Children.Add(new pom());
        }

        private void Nueva_Entrada(object sender, RoutedEventArgs e)
        {
            this.svContainer.Content = null;

            stackPanel2.Children.Add(Guardar);
            stackPanel2.Children.Add(Guardar_Salir);



           /* try
            {
                stackPanel2.Children.Add(Guardar);
                stackPanel2.Children.Add(Guardar_Salir);
                Guardar.Click += new RoutedEventHandler(Guardar_Click);
                Guardar_Salir.Click += new RoutedEventHandler(Guardar_Salir_Click);
            }
            catch (Exception ex)
            {
                
            }
            */

            //<Button Content="Eliminar" Height="30" Name="button2" Width="75" Margin="10,0,0,0" FontFamily="Arial" Background="#e5e5e5" BorderBrush="#FFE5E5E5" />
            
            //Para fines de prueba mandaremos a llamar a POM con ID's
            Despliega1 = new pom(2,2);

            //Despliega1 = new pom();
            svContainer.Content = Despliega1;
            this.button1.Visibility = Visibility.Hidden;
        }

        private void btn_Entradas_Click(object sender, RoutedEventArgs e)
        {
            this.svContainer.Content = null;
        }

        private void btn_Salidas_Click(object sender, RoutedEventArgs e) 
        {
            this.svContainer.Content = null;
        }

        private void btn_ReciboAlmacen_Click(object sender, RoutedEventArgs e) 
        {
            this.svContainer.Content = null;
            _despliega2 = new UI.UIGrid(svContainer);
            button1.Visibility = Visibility.Visible;
            stackPanel2.Children.Remove(Guardar);
            stackPanel2.Children.Remove(Guardar_Salir);
        }


        private void Guardar_Salir_Click(object sender, RoutedEventArgs e)
        {
            Guardar_Click(sender, e);
            if (ban)
            {
                btn_ReciboAlmacen_Click(sender, e);
                ban = false;
                stackPanel2.Children.Remove(Guardar);
                stackPanel2.Children.Remove(Guardar_Salir);
                button1.Visibility = Visibility.Visible;
                
            }
            
        }

        private void Guardar_Click(object sender, RoutedEventArgs e)
        {
            
            ClaseItems ci = new ClaseItems();

            //Para fines de pruebas
            
            ci.ID_Lote = Despliega1.ID_Lote;
            ci.ID_POM = Despliega1.ID_POM;
            ci.ID_Proveedor = Despliega1.Proveedor;

            string campo = "";
            Double aux = 0;

            try
            {

                campo = "Lote";
                if (Despliega1.textBox1.Text.Equals(""))
                {
                    MessageBox.Show("El campo " + campo + "  está vacio.");
                    return;
                }

                campo = "Factura";
                if (Despliega1.textBox2.Text.Equals(""))
                {
                    MessageBox.Show("El campo " + campo + "  está vacio.");
                    return;
                }

                campo = "PedimentoImporte";
                aux = Double.Parse(Despliega1.textBox3.Text);
                campo = "IVAFactura";
                aux = Double.Parse(Despliega1.textBox6.Text);
                campo = "Pedimento";
                aux = Double.Parse(Despliega1.textBox4.Text);
                campo = "% IVA Compra";
                aux = Double.Parse(Despliega1.textBox7.Text);
                campo = "Total Factura";
                aux = Double.Parse(Despliega1.textBox8.Text);

            }
            catch (Exception Ex)
            {

                MessageBox.Show("El campo " + campo + " contiene caracteres inválidos o está vacio.");
                return;
            }

            
           

            List<ListaITEM> ListaFinal = new List<ListaITEM>();
            Despliega1.textBox5.Text = "0";
            for (int i = 0; i < Despliega1.articulos_dg2_total.Count; i++)
            {

                ListaITEM ItemAux = new ListaITEM();
                //Por cuestiones de pruebas ID_Track = 1 ID_Item = 1

                ItemAux.ID_Item = 1;
                ItemAux.ID_Track = 1;
                ItemAux.Serie = Despliega1.articulos_dg2_total[i].Serie;
                ItemAux.Sku = Despliega1.articulos_dg2_total[i].Sku;
                ItemAux.Costo_Final = Despliega1.articulos_dg2_total[i].Costo_Final;
                ItemAux.Is_Arrendado = Despliega1.articulos_dg2_total[i].Is_Arrendado;

                //ItemAux.ID_Lot_Art = 1;

                ItemAux.ID_Articulo = Despliega1.articulos_dg2_total[i].ID_Articulo;
                ItemAux.Nombre_Articulo = Despliega1.articulos_dg2_total[i].Nombre_Articulo;
                ItemAux.Cantidad = Despliega1.articulos_dg2_total[i].Cantidad;
                ItemAux.costoU = Despliega1.articulos_dg2_total[i].costoU;
                ItemAux.IVA = Despliega1.articulos_dg2_total[i].IVA;

                ItemAux.ID_Moneda = Despliega1.articulos_dg2_total[i].ID_Moneda;
                ItemAux.Moneda =  Despliega1.articulos_dg2_total[i].Moneda;
                ItemAux.descuento = Despliega1.articulos_dg2_total[i].descuento;
                ItemAux.TC = Despliega1.articulos_dg2_total[i].TC;
                ItemAux.ID_Item = Despliega1.articulos_dg2_total[i].ID_Item;

                ListaFinal.Add(ItemAux);
                //txt Importe de la Factura            
                if (!Despliega1.textBox5.Text.ToString().Equals(""))
                    Despliega1.textBox5.Text = (Double.Parse(Despliega1.textBox5.Text.ToString()) + Despliega1.articulos_dg2_total[i].costoU).ToString();
                else
                    Despliega1.textBox5.Text = Despliega1.articulos_dg2_total[i].costoU.ToString();
            }

            ci.ListaRecibida = ListaFinal;
            ci.insertITEM();
            ban = true;
        }

        private void svContainer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {

        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }
    }
}
